
#include "../include/server.h"

int main()
{
   //socket
   int iServer=socket(AF_INET,SOCK_STREAM,0);
   if(-1==iServer)
   {
       printf("create socket error\r\n");
       return -1;
   }
   printf("create socket ok,iServer=%d\r\n",iServer);
   //bind
   struct sockaddr_in stServer;
   stServer.sin_family=AF_INET;
   stServer.sin_port=htons(8888);
   stServer.sin_addr.s_addr=INADDR_ANY; //inet_addr("127.0.0.1");
   int ret=bind(iServer,(struct sockaddr *)&stServer,sizeof(struct sockaddr));
   if(-1==ret)
   {
	   printf("bind error\r\n");
	   return -1;
   }
   printf("bind ok\r\n");
   //listen
   ret=listen(iServer,5);
   if(-1==ret)
   {
	   return -1;
   }
   printf("listen ok\r\n");
   //accept
   struct sockaddr_in stClient;
   socklen_t len=sizeof(struct sockaddr_in);
   memset( &list , 0 , sizeof(list) );
   int stlen;
   int i;
   int listSize;
   struct epoll_event ev;
   int epfd = epoll_create( 10 );
   ev.data.fd = iServer;
   ev.events = EPOLLIN;
   epoll_ctl( epfd , EPOLL_CTL_ADD , iServer , &ev );
   list.size = 1;
   char *pPath = NULL;
   sqlite3 *pdb = NULL;
   char *pErrMsg = NULL;
   char dbName[20] = { 0 };
   char tbName[20] = { 0 };
   char currentUser[50] = { 0 };
   char tempbuf[BUF_SIZE];
   int argc;
   char *argv[10] = { 0 };
   int iResult;
   do
   {
	   iResult = sqlite3_open( "userinfo" , &pdb );
	   strcpy( dbName , "userinfo" );
   }
	while( SQLITE_OK != iResult || NULL == pdb );//创建或者打开用户信息表
	do
	{
	   
	   iResult = sqlite3_exec( pdb , 
	   "create table if not exists userinfotb ( account Integer not null primary key , password Integer not null , 	   nickname char , days Integer not null  );" , NULL , NULL , &pErrMsg );
	   strcat( tbName , "userinfotb" );
	}
	while( SQLITE_OK != iResult );//创建用户信息表
    while(1)
   {
        puts( "rount again" );
		listSize = list.size;
		printf( "total client %d\n" , listSize - 1 );
		int nfds = epoll_wait( epfd , list.events , list.size , -1 );
		for( i = 0 ; i < nfds ; i++ )
		{
			if( list.events[i].data.fd == iServer )
			{
				if( list.size <= BUF_SIZE )
				{
	             	memset( &stClient , 0 , len );
                    ret  = accept( iServer , (struct sockaddr*)&stClient , &stlen );
					if( ret <= 0  )
					{
						printf( "accept error\n" );
						continue;
					}
					ev.data.fd = ret;
					ev.events =  EPOLLIN;
					epoll_ctl( epfd , EPOLL_CTL_ADD , ret , &ev );
					list.size++;
				}
				else
				{
					printf( "maxinum connections\n" );
				}  
			}
			else
			{
					char buf[BUF_SIZE] = { 0 };
					ret = recv( list.events[i].data.fd , buf , BUF_SIZE , 0 );
					if( ret <= 0 )
					{
						printf( "send %d errror\n" , list.events[i].data.fd );
						close( list.events[i].data.fd );
						continue;
					}
					int flag;
					int num;
					char tempusername[30];
					sqlite3 *temppdb = NULL;
	                char *pErrMsg = NULL;
					char tempbuf[BUF_SIZE] = { 0 };
					switch( *buf )
					{
						case '0': //客户端的登录验证请求
						    flag = loginCheck( buf + 1 , pdb , tbName , currentUser);// 登录验证
							if( 0 == flag )
							{ 
						        printf( "log check success\n" );
								memset( buf , 0 , BUF_SIZE );
								sprintf( buf , "10000");
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d errror\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
						    else
							{
								printf( "log check failed\n" );
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "10001" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d errror\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
							
							break;
						case '1': //客户端的新用户注册请求
						    flag = userRegistration( buf + 1 , pdb , tbName );// 新用户注册
							if( 0 == flag )
							{
								memset( buf , 0 , BUF_SIZE );
								sprintf( buf , "11000");
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d errror\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
							else
							{
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "11001" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d errror\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
							break;
						case '2':
							//服务器根据客户端的日期和用户名请求账单数据
							flag = userinfoSearch(  buf + 1  );
							ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
							if( ret <= 0 )
							{
								printf( "send %d error\n" , list.events[i].data.fd );
								close( list.events[i].data.fd );
							}
							break;
						case '3':
						    //新增加当天的账目信息
							flag = addNewPost( buf + 1 );
							if( -1 == flag )
					        {
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "31001" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d error\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
									
								}
							}
							else
							{
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "31000" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d error\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
							break;
						case '4':
						
							break;
						case '5':
							printf("%s\n" , buf );
						    flag = updateUserPasswd( pdb , tbName  , buf + 1 );
							if( -1 == flag )
					        {
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "51001" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d error\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
									
								}
							}
							else
							{
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "51000" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d error\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
							break;
							break;
						case '6': //修改昵称
						    printf("%s\n" , buf );
						    flag = updateUserNickname( pdb , tbName  , buf + 1 );
							if( -1 == flag )
					        {
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "61001" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d error\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
									
								}
							}
							else
							{
								memset( buf , 0 , BUF_SIZE );
								strcat( buf , "61000" );
								ret = send( list.events[i].data.fd , buf , BUF_SIZE , 0 );
								if( ret <= 0 )
								{
									printf( "send %d error\n" , list.events[i].data.fd );
									close( list.events[i].data.fd );
								}
							}
							break;
						default:
							break;
					}
				
				
				
				
				
				
			} 
		}
   }
   printHello();
   return 0;
}